package com.mls.fivedistrict.util;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.mapper.Condition;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.exception.ParamException;

import java.util.Collection;
import java.util.List;

/**
 * Created by lixixi on 2018/6/3.
 */
public class PageFilterUtils {

    public static void recoWrapper(PageFilter pageFilter) {
        List<PageFilter.FilterBean> filterBeans = pageFilter.getFilters();
        if (filterBeans != null) {
            Condition condition = Condition.create();
            for (PageFilter.FilterBean filterBean : filterBeans) {
                disposeFilter(filterBean, condition);
            }
            pageFilter.setWrapper(condition);
        }
    }

    private static void disposeFilter(PageFilter.FilterBean filterBean, Condition condition) {
        if (StringUtils.isEmpty(filterBean.getOperator())) {
            return;
        }

        switch (filterBean.getOperator()) {
            case "eq":
                //相等
                condition.eq(filterBean.getKey(), filterBean.getValue());
                break;
            case "ne":
                //不等于<>
                condition.ne(filterBean.getKey(), filterBean.getValue());
                break;
            case "gt":
                //大于>
                condition.gt(filterBean.getKey(), filterBean.getValue());
                break;
            case "ge":
                //大于等于>=
                condition.ge(filterBean.getKey(), filterBean.getValue());
                break;
            case "lt":
                //小于<
                condition.lt(filterBean.getKey(), filterBean.getValue());
                break;
            case "le":
                //小于等于<=
                condition.le(filterBean.getKey(), filterBean.getValue());
                break;
            case "like":
                //模糊查询 LIKE
                condition.like(filterBean.getKey(), String.valueOf(filterBean.getValue()));
                break;
            case "notLike":
                //模糊查询 NOT LIKE
                condition.notLike(filterBean.getKey(), (String) filterBean.getValue());
                break;
            case "in":
                //IN 查询
                condition.in(filterBean.getKey(), (Collection<?>) filterBean.getValue());
                break;
            case "notIn":
                //NOT IN 查询
                condition.notIn(filterBean.getKey(), filterBean.getValue());
                break;
            case "isNull":
                //NULL 值查询
                condition.isNull(filterBean.getKey());
                break;
            case "isNotNull":
                //IS NOT NULL
                condition.isNotNull(filterBean.getKey());
                break;
            case "groupBy":
                //groupBy
                condition.groupBy(filterBean.getKey());
                break;
            case "having":
                //HAVING 关键词
                condition.having(filterBean.getKey(), filterBean.getValue());
                break;
            case "orderBy":
                condition.orderBy(filterBean.getKey());
                break;
            case "orderAsc":
                condition.orderAsc((Collection<String>) filterBean.getValue());
                break;
            case "orderDesc":
                condition.orderDesc((Collection<String>) filterBean.getValue());
                break;
            case "exists":
                condition.exists(filterBean.getKey());
                break;
            case "notExists":
                condition.notExists(filterBean.getKey());
                break;
            case "between":
                List<Object> values = (List<Object>) filterBean.getValue();
                if (values.size() != 2) {
                    throw new ParamException("参数错误");
                }
                condition.between(filterBean.getKey(), values.get(0), values.get(1));
                break;
            case "notBetween":
                values = (List<Object>) filterBean.getValue();
                if (values.size() != 2) {
                    throw new ParamException("参数错误");
                }
                condition.notBetween(filterBean.getKey(), values.get(0), values.get(1));
                break;
        }
    }
}
